package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.ClassSearchCriteria;
import com.nwpu.coursesupport.criteria.ClassViewSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.exception.TransactionException;
import com.nwpu.coursesupport.service.ClassService;
import com.nwpu.coursesupport.service.param.ClassServiceParam;
import com.nwpu.coursesupport.tool.JSONUtils;

@Controller
@RequestMapping("/class")
public class ClassController extends AbstractController {

	@Autowired
	private ClassService classService;
	
	@RequestMapping("/courseView/{id}")
	public String courseView(@PathVariable("id") Integer id,HttpServletRequest request){
		JSONResult result=this.classService.courseView(id);
		request.setAttribute("data", this.parseData(result));
		return "/teapage/courseIndex";
	}
    
	@RequestMapping("/ajaxCourseView/{id}")
	@ResponseBody
	public JSONResult ajaxCourseView(@PathVariable("id") Integer id,HttpServletRequest request){
		JSONResult result=this.classService.courseView(id);
		int courseId=(int) result.getData().get("courseId");
		request.setAttribute("courseId", courseId);
		return result;
	}
	
	
    @RequestMapping("/detail")
    public String detail(@RequestParam("id") Integer id,HttpServletRequest request) {
    	JSONResult result=this.classService.detail(id);
        request.setAttribute("data", parseData(result));
        return "/teapage/classDetail";
    }

    @RequestMapping("/ajaxDetail")
    @ResponseBody
    public JSONResult ajaxDetail(@RequestParam("id") Integer id) {
    	JSONResult result=this.classService.detail(id);
        return result;
    }
    
    //通过班级码获取班级信息
    @RequestMapping("/ajaxClassViewByCode")
    @ResponseBody
    public JSONResult ajaxGetClassView(@RequestParam("data") String data){
    	JSONResult rst=new JSONResult();
        rst.fail();
        ClassViewSearchCriteria c=JSONUtils.json2pojo(data, ClassViewSearchCriteria.class);
    	if(c==null){
    		return rst.fail("参数有误");
    	}
        JSONResult result = this.classService.findClassView(c);
        return result;
    }
   /* @RequestMapping("/list")
    public String list(@RequestBody ClassSearchCriteria sc,HttpServletRequest request) {
    	JSONResult result = classService.list(sc);
        request.setAttribute("data", parseData(result));
        return "/classList";
    }*/

    @RequestMapping("/ajaxListData")
    @ResponseBody
    public JSONResult ajaxListData(@RequestParam("data") String data) {
    	JSONResult rst=new JSONResult();
        rst.fail();
        ClassSearchCriteria c=JSONUtils.json2pojo(data, ClassSearchCriteria.class);
    	if(c==null){
    		return rst.fail("参数有误");
    	}
        JSONResult result = this.classService.list(c);
        return result;
    }
    
    
    @RequestMapping("/ajaxCreate")
    @ResponseBody
    public JSONResult ajaxCreate(@RequestParam("data") String data) {
        JSONResult rst=new JSONResult();
        rst.fail();
    	try{
        	ClassServiceParam.CreateParam c=JSONUtils.json2pojo(data, ClassServiceParam.CreateParam.class);
        	if(c==null){
        		return rst.fail("参数有误");
        	}
        	rst = classService.create(c);
    	}catch(TransactionException e){//如果出现失败，会抛异常，同时会回滚事物（因为添加）
        	logger.error("抛出异常，添加班级失败");
        	rst.fail("添加班级出现异常");
        }
        return rst;
    }

    @RequestMapping("/ajaxUpdate")
    @ResponseBody
    public JSONResult ajaxUpdate(@RequestParam("data") String data) {
    	JSONResult rst=new JSONResult();
        rst.fail();
        ClassServiceParam.UpdateParam p=JSONUtils.json2pojo(data, ClassServiceParam.UpdateParam.class);
    	if(p==null){
    		return rst.fail("参数有误");
    	}
        JSONResult result = this.classService.update(p);
        return result;
    }

}
